import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/res/image.gen.dart';

class MyAppBar extends StatelessWidget {
  String title;
  String titleRight;
  bool isCenterImage;
  bool isText;
  bool isImage;
  bool isPopup;
  IconData? iconData;
  Function? confirm;
  Function? poupConfirm;
  String? poupTitle;

  MyAppBar({
    this.title="",
    this.titleRight = "",
    this.isCenterImage=false,
    this.isText = false,
    this.isImage = false,
    this.isPopup = false,
    this.iconData,
    this.confirm,
    this.poupConfirm,
    this.poupTitle,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 10, right: isText?10:0),
      color: Colors.black,
      height: 60,
      alignment: Alignment.center,
      child: Row(
        children: [
          InkWell(
            child: Image(image: AssetImage(ImageRes.back), height: 25),
            onTap: () {
              Get.back();
            },
          ),
          SizedBox(
            width: 25,
          ),
          Offstage(
            offstage:isCenterImage,
            child: Container(
              padding: EdgeInsets.only(
                  bottom: (GetPlatform.isWeb && GetPlatform.isMobile) ? 8 : 0),
              child: Text(title,
                  style: TextStyle(fontSize: 20, color: Colors.white)),
            ),
          ),
          Offstage(
            offstage:!isCenterImage,
            child: Expanded(
              child:Image(image: AssetImage(ImageRes.yinzhi_log), height: 25),
            )),
          // Expanded(child: Text("")),
          isText
              ? InkWell(
                  onTap: () {
                    confirm?.call();
                  },
                  child: Text(
                    titleRight,
                    style: TextStyle(fontSize: 15, color: Colors.white),
                  ))
              : Offstage(
                  offstage: isImage,
                  child: isPopup ? _popup() : _iconButton(),
                )
        ],
      ),
    );
  }

  _popup() {
    return PopupMenuButton(
        icon: Icon(
          Icons.menu,
          color: Colors.white,
        ),
        onSelected: (value) {
          switch (value) {
            case 1:
              poupConfirm?.call();
              break;
          }
        },
        itemBuilder: (context) {
          return <PopupMenuEntry<int>>[
            PopupMenuItem(
              child: Text(poupTitle!),
              value: 1,
            ),
          ];
        });
  }

  _iconButton() {
    return IconButton(
      icon: Icon(
        iconData,
        color: Colors.white,
      ),
      alignment: Alignment.centerRight,
      onPressed: () {
        confirm?.call();
      },
    );
  }
}
